#include "grate/calculator.h"

#include "grate/util/common.h"

namespace growth_rate {

/* static */
double Calculator::get_language_growth_rate(
    LexminAntiDictionary *AD, bool verbosity,
    const EigenvalueOptions &options) {
  CHECK(AD);
  LexminTrie *trie = AD->build_trie();
  if (verbosity) {
    Report("Extra memory used for building a trie: %ld\n",
           AD->extra_memory_usage());
    Report("Nodes in lexmin trie: %ld\n", trie->size());
    Report("Words in lexmin trie: %ld\n", trie->number_of_leaf_nodes());
  }

  // Delete antidictionary before creating a DFA to save memory.
  delete AD;
  LexminDFA *dfa = new LexminDFA(trie);
  delete trie;
  if (verbosity) {
    Report("Lexmin DFA constructed!\n");
  }

  size_t iterations_used;
  double result = EigenvalueCalculator::get_max_eigenvalue(dfa, options,
                                                           &iterations_used);
  if (verbosity) {
    Report("Iterations used: %ld\n", iterations_used);
    Report("Growth rate calculated: %.12lf\n", result);
  }
  return result;
}

}  // namespace growth_rate
